package org.example.main.multi_threading;

/**
 * @Author: WangShengJie
 * @Date: 2025/6/1
 */
public class DeadLockDemo {

    private final static String RESOURCE_A = "A";
    private final static String RESOURCE_B = "B";

    /**
     * 当前程序会死锁
     * 1、使用jps查看进行号
     * 2、使用 jstack+进程号  查看当前进程堆栈信息
     *
     */
    public static void main(String[] args) {
        Thread threadA = new Thread(() -> {
            synchronized (RESOURCE_A) {
                System.out.println("threadA 获取到了 RESOURCE_A");
                try {
                    Thread.sleep(100);
                    synchronized (RESOURCE_B) {
                        System.out.println("threadA 获取到了 RESOURCE_B");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread threadB = new Thread(() -> {
            synchronized (RESOURCE_B) {
                System.out.println("threadB 获取到了 RESOURCE_B");
                try {
                    Thread.sleep(100);
                    synchronized (RESOURCE_A) {
                        System.out.println("threadB 获取到了 RESOURCE_A");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        threadA.start();
        threadB.start();

    }
}
